﻿package com.game.data.vo.attribute
{
	import base.BaseAddEffect;
	
	import com.game.data.constant.AttribEffectType;
	import com.game.data.vo.AddEffectVO;
	import com.game.manager.AnimationManager;
	import base.BaseHero;
	
	import com.dusk.game.Logger;
	import com.dusk.util.ArrayUtil;
	import com.game.data.constant.AttributeName;
	import com.game.data.constant.MaxAttribConst;
	import com.game.data.vo.EquipmentVO;
	import com.game.view.display.AnimationClip;
	import com.game.view.display.AnimationLoopType;
	
	public class RoleAttribVO extends BaseAttribVO
	{
		
		public function RoleAttribVO(bh:BaseHero)
		{
			super();
			_attribEffect = [];
			who = bh;
		}
		
		private var who:BaseHero;
		public var tjbg_revive:Boolean = false;
		private var _attribEffect:Array;
		public var needExp:int;
		private var _framer:int = 0;
		private var _level:int;
		private var _energy:Number = 0;
		
		public function get energy():Number
		{
			return _energy;
		}
		
		public function set energy(value:Number):void
		{
			if (value < 0)
			{
				value = 0;
			}
			if (value > 100)
			{
				value = 100;
			}
			_energy = value;
		}
		
		public function get level():int
		{
			return _level;
		}
		
		public function set level(value:int):void
		{
			_level = value;
			who.getPlayer().level = value;
			refreshNeedExp();
		}
		
		/**
		 * 获取经验值
		 */
		public function get exp():int
		{
			return who.getPlayer().exp;
		}
		
		public function set exp(value:int):void
		{
			if (!who || !who.getPlayer())
			{
				return;
			}
			who.getPlayer().exp = value;
			if (level < MaxAttribConst.MaxRoleLevel)
			{
				judgeUpGrade();
			}
		}
		
		override public function update():void
		{
			if (!who || !who.curAddEffect)
			{
				return;
			}
			if (_framer++ % 30 == 0)
			{
				if (gc.flowerSoul.getSoulLevelByType("emp") > 0)
				{
					var data:Object = gc.flowerSoul.getProp_emp();
					if (mp / mpMax < data.treshold)
					{
						cureMp(mpHeal * data.rate);
					}
					else
					{
						cureMp(mpHeal);
					}
				}
				else
				{
					cureMp(mpHeal);
				}
				cureHp(hpHeal);
			}
			renderAddEffect();
		}
		
		/**
		 * 增加经验值
		 * @param value
		 */
		public function addExp(value:int):void
		{
			exp += value;
		}
		
		/**
		 * 添加装备
		 * @param vo
		 */
		public function addEquip(vo:EquipmentVO):void
		{
			if (vo != null)
			{
				hpMax += vo.getAttributeValue(AttributeName.HP);
				mpMax += vo.getAttributeValue(AttributeName.MP);
				atk += vo.getAttributeValue(AttributeName.ATK);
				def += vo.getAttributeValue(AttributeName.DEF);
				hpHeal += vo.getAttributeValue(AttributeName.HP_HEAL)
				mpHeal += vo.getAttributeValue(AttributeName.MP_HEAL);
				crit += vo.getAttributeValue(AttributeName.CRIT);
				miss += vo.getAttributeValue(AttributeName.MISS);
				bloodThirst += vo.getAttributeValue(AttributeName.BLOOD_THIRST);
				magicThirst += vo.getAttributeValue(AttributeName.MAGIC_THIRST);
				hit += vo.getAttributeValue(AttributeName.HIT);
				magicDef += vo.getAttributeValue(AttributeName.MAGIC_DEF);
				critDamage += vo.getAttributeValue(AttributeName.CRIT_DAMAGE);
				deCritDamage += vo.getAttributeValue(AttributeName.DE_CRIT_DAMAGE);
			}
		}
		
		/**
		 * 移除装备
		 * @param vo 装备
		 * @param limitMaxValue 是否限制最大值，特指血量和魔法值
		 */
		public function removeEquip(vo:EquipmentVO, limitMaxValue:Boolean = true):void
		{
			if (vo != null)
			{
				hpMax -= vo.getAttributeValue(AttributeName.HP);
				mpMax -= vo.getAttributeValue(AttributeName.MP);
				atk -= vo.getAttributeValue(AttributeName.ATK);
				def -= vo.getAttributeValue(AttributeName.DEF);
				hpHeal -= vo.getAttributeValue(AttributeName.HP_HEAL)
				mpHeal -= vo.getAttributeValue(AttributeName.MP_HEAL);
				crit -= vo.getAttributeValue(AttributeName.CRIT);
				miss -= vo.getAttributeValue(AttributeName.MISS);
				bloodThirst -= vo.getAttributeValue(AttributeName.BLOOD_THIRST);
				magicThirst -= vo.getAttributeValue(AttributeName.MAGIC_THIRST);
				hit -= vo.getAttributeValue(AttributeName.HIT);
				magicDef -= vo.getAttributeValue(AttributeName.MAGIC_DEF);
				critDamage -= vo.getAttributeValue(AttributeName.CRIT_DAMAGE);
				deCritDamage -= vo.getAttributeValue(AttributeName.DE_CRIT_DAMAGE);
				if (limitMaxValue && hpMax < hp)
				{
					hp = hpMax;
				}
				if (limitMaxValue && mpMax < mp)
				{
					mp = mpMax;
				}
			}
		}
		
		public function removeAllEquipAndPassive():void
		{
			if (gc.flowerSoul.getSoulLevelByType("hit") > 0)
			{
				var _loc1_:* = gc.flowerSoul.getProp_hit();
				hit -= _loc1_.key;
			}
			removeAllEquip();
			removeAllPassive();
		}
		
		public function initAll():void
		{
			if (gc.flowerSoul.getSoulLevelByType("hit") > 0)
			{
				var _loc1_:* = gc.flowerSoul.getProp_hit();
				hit += _loc1_.key;
			}
			addAllPassive();
			addAllEquip();
		}
		
		public function destroy():void
		{
			removeAllEquipAndPassive();
			who = null;
		}
		
		/**
		 * 添加被动技能属性
		 * @param skillType 技能种类
		 * @param skillLevel 技能等级
		 * @param needRemoveLowLevel 是否需要移除低等级技能
		 */
		public function addPassive(skillType:uint, skillLevel:int, needRemoveLowLevel:Boolean = false):void
		{
			if (needRemoveLowLevel)
			{
				removePassive(skillType, skillLevel - 1);
			}
			analysePassiveSkill(skillType, skillLevel, 1);
		}
		
		/**
		 * 移除被动技能属性
		 * @param skillType 技能种类
		 * @param skillLevel 技能等级
		 */
		public function removePassive(skillType:uint, skillLevel:int):void
		{
			analysePassiveSkill(skillType, skillLevel, -1);
		}
		
		private function refreshNeedExp():void
		{
			if (_level < 7)
			{
				needExp = 135 + 10 * (_level - 1);
			}
			else if (_level < 13)
			{
				needExp = (625 + 50 * (_level - 7));
			}
			else if (_level < 19)
			{
				needExp = (1950 + 100 * (_level - 13));
			}
			else
			{
				needExp = (5000 + 5000 * (_level - 19));
			}
		}
		
		public function removeAllAttribEffect():void
		{
			while (_attribEffect.length > 0)
			{
				removeAttribEffect((_attribEffect[0] as AddEffectVO).type);
			}
		}
		
		private function addAttribEffect(data:AddEffectVO):void
		{
			if (ArrayUtil.contains(_attribEffect, data.type))
			{
				//目前不考虑效果可以添加多层的情况，如破军戒指的叠层
				//以后如果加进来的话可能会删除此判断
				Logger.log("要添加的效果已存在");
				return;
			}
			var add:Boolean = true;
			switch (data.type)
			{
				case BaseAddEffect.PET_SMJC:
					modifyHpKeepPercent(data.value);
					Logger.log("添加宠物buff-生命加成;生命提高" + data.value + "点");
					break;
				case BaseAddEffect.PET_MFJC:
					modifyMpKeepPercent(data.value);
					Logger.log("添加宠物buff-魔法加成;魔法提高" + data.value + "点");
					break;
				case BaseAddEffect.PET_GJJC:
					atk += data.value;
					Logger.log("添加宠物buff-攻击加成;攻击提高" + data.value + "点");
					break;
				case BaseAddEffect.PET_FYJC:
					def += data.value;
					Logger.log("添加宠物buff-防御加成;防御提高" + data.value + "点");
					break;
				case BaseAddEffect.MAGIC_FLOWER_ADDBUFF:
					magicDef += data.value;
					def += data.value;
					Logger.log("添加魂莲法宝buff-加成;防御、魔抗提高" + data.value + "点");
					break;
				case AttribEffectType.ROLE1_PASSIVE_KBJQ:
					bloodThirst += data.value;
					Logger.log("添加悟空被动技能-嗜血,吸血提高" + data.value + "点");
					break;
				case "role3_rj":
					def += 10 * data.value;
					magicDef += data.value;
					Logger.log("添加八戒被动技能-刃甲,防御、魔抗提高");
					break;
				default:
					Logger.log("未知的buff或无需做处理，请检查对应参数");
					add = false;
					break;
			}
			if (add)
			{
				_attribEffect.push(data);
			}
		}
		
		/**
		 * 传入需要移除的effect类型，会自动移除所有的同名effect
		 * @param type int
		 */
		private function removeAttribEffect(type:int):void
		{
			var arr:Array = _attribEffect.filter(function (a:AddEffectVO,...rest):Boolean
			{
				return a.type == type;
			});
			for each (var eff:AddEffectVO in arr)
			{
				switch (eff.type)
				{
					case BaseAddEffect.PET_SMJC:
						modifyHpKeepPercent(-eff.value);
						Logger.log("宠物buff-魔法加成-到期,血量回调" + eff.value + "点");
						break;
					case BaseAddEffect.PET_MFJC:
						modifyMpKeepPercent(-eff.value);
						Logger.log("宠物buff-魔法加成-到期,魔法回调" + eff.value + "点");
						break;
					case BaseAddEffect.PET_GJJC:
						atk -= eff.value;
						Logger.log("宠物buff-攻击加成-到期,攻击回调" + eff.value + "点");
						break;
					case BaseAddEffect.PET_FYJC:
						def -= eff.value;
						Logger.log("宠物buff-防御加成-到期,防御回调" + eff.value + "点");
						break;
					case BaseAddEffect.MAGIC_FLOWER_ADDBUFF:
						magicDef -= eff.value;
						def -= eff.value;
						Logger.log("魂莲法宝buff-攻击加成-到期,防御、魔抗回调" + eff.value + "点");
						break;
					case AttribEffectType.ROLE1_PASSIVE_KBJQ:
						bloodThirst -= eff.value;
						Logger.log("移除被动技能-嗜血,吸血降低" + (eff.value) + "点");
						break;
					case "role3_rj":
						def -= 10 * eff.value;
						magicDef -= eff.value;
						Logger.log("移除被动技能-刃甲,防御、魔抗回调");
						break;
					default:
						Logger.log("未知的buff或无需做处理，请检查对应参数");
						break;
				}
			}
			ArrayUtil.removeElementFromAnother(arr, _attribEffect);
		}
		
		private function hasAttribEffect(type:int):Boolean
		{
			for each(var eff:AddEffectVO in _attribEffect)
			{
				if (eff.type == type)
				{
					return true;
				}
			}
			return false;
		}
		
		/**
		 * 在每一帧检测新的effect，例如新学了狂暴，就会增加新的效果
		 * 所有对于角色属性有改变的effect都在这里处理
		 */
		private function renderAddEffect():void
		{
			const need:Array = [
			]
			for each(var eff:int in need)
			{
				var data:AddEffectVO = who.curAddEffect.getEffectByType(eff);
				if (data)
				{
					if (!hasAttribEffect(data.type))
					{
						addAttribEffect(data);
					}
					else if (who.curAddEffect.getEffectLeftTime(data.frame) == 0)
					{
						removeAttribEffect(data.type);
					}
				}
			}
			var vo:AddEffectVO;
			if (who.getPlayer().roleId == 1)
			{
				//这里其实有一个很大的bug
				//先学习技能，然后升到很高等级，等待buff挂上之后再来遗忘
				//那么在挂上buff的时候获取到技能等级不为0，但是在移除buff的时候，技能等级肯定是0
				//这样一来，我只需要在一个关卡里面一直刷吸血就好了（为数不多的解决方法就是限制孟婆药剂的使用）
				if (who.getPlayer().getHasEquipSkillById("kbjq"))
				{
					if(!hasAttribEffect(AttribEffectType.ROLE1_PASSIVE_KBJQ))
					{
						vo = new AddEffectVO();
						vo.type = AttribEffectType.ROLE1_PASSIVE_KBJQ;
						vo.value = 3 + who.getPlayer().getStudiedSkillBySkillId("kbjq").skillLevel;
						addAttribEffect(vo);
					}
				}
				else if (hasAttribEffect(AttribEffectType.ROLE1_PASSIVE_KBJQ))
				{
					removeAttribEffect(AttribEffectType.ROLE1_PASSIVE_KBJQ);
				}
			}
//			if (who is Role3)
//			{
//				const role3_rj:String = "role3_rj";
//				if (who.getPlayer().findSkillIsInTheSkillAry("rj"))
//				{
//					if (!ArrayUtil.contains(_attribEffect, role3_rj))
//					{
//						addAttribEffect({
//							name : role3_rj,
//							value: who.getPlayer().returnSkillLevelBySkillName("rj")
//						});
//					}
//				}
//				else if (ArrayUtil.contains(_attribEffect, role3_rj))
//				{
//					removeAttribEffect({
//						name : role3_rj,
//						value: who.getPlayer().returnSkillLevelBySkillName("rj")
//					});
//				}
//			}
		}
		
		/**
		 * 判断是否能升级
		 */
		private function judgeUpGrade():void
		{
			if (!who || !who.getPlayer())
			{
				return;
			}
			if (exp >= needExp)
			{
				var i:int = needExp;//先备份一个值防止出现升级后经验值变为负数
				level += 1;//level改变时升级所需要的经验值随即改变
				exp -= i;
				who.upGrade();
				var eff:AnimationClip = AnimationManager.getAnimationClip("RoleLevelUpMc");
				eff.loopType = AnimationLoopType.REMOVE_WHEN_END;
				eff.x = who.getLocalCollideCenter().x;
				eff.y = 0;
				who.effectContainer.addChild(eff);
			}
		}
		
		/**
		 * 添加所有装备属性
		 */
		private function addAllEquip():void
		{
			if (!who || !who.getPlayer())
			{
				return;
			}
			for each (var equip:EquipmentVO in who.getPlayer().wearEquip.getAllEquip())
			{
				addEquip(equip);
			}
			setFullState();
		}
		
		/**
		 * 移除所有装备属性
		 */
		private function removeAllEquip():void
		{
			if (!who || !who.getPlayer())
			{
				return;
			}
			for each (var equip:EquipmentVO in who.getPlayer().wearEquip.getAllEquip())
			{
				removeEquip(equip);
			}
		}
		
		/**
		 * 移除所有被动技能属性
		 */
		private function removeAllPassive():void
		{
			if (!who || !who.getPlayer())
			{
				return;
			}
			for (var i:int = 0; i < who.getPlayer().passiveSkill.length; i++)
			{
				removePassive(i, who.getPlayer().passiveSkill[i]);
			}
		}
		
		/**
		 * 添加所有被动技能属性
		 */
		private function addAllPassive():void
		{
			if (!who || !who.getPlayer())
			{
				return;
			}
			for (var i:int = 0; i < who.getPlayer().passiveSkill.length; i++)
			{
				addPassive(i, who.getPlayer().passiveSkill[i]);
			}
		}
		
		/**
		 * 分析被动技能属性,对角色属性进行更改
		 * @param skillType 被动技能种类
		 * @param skillLevel 被动技能等级
		 * @param addOrMinus 定义属性增加还是减少，值限制为1或者-1
		 */
		private function analysePassiveSkill(skillType:uint, skillLevel:uint, addOrMinus:int):void
		{
			if (skillLevel == 0)
			{
				return;
			}
			var data:int = 0;
			var rate:Number = 0;
			switch (skillType)
			{
				case 0:
					data = skillLevel * 100 * addOrMinus;
					rate = getHpPercent();
					hpMax += data;
					hp = hpMax * rate;
					break;
				case 1:
					data = skillLevel * 100 * addOrMinus;
					rate = getMpPercent();
					mpMax += data;
					mp = mpMax * rate;
					break;
				case 2:
					data = skillLevel * 1 * addOrMinus;
					crit += data;
					break;
				case 3:
					data = skillLevel * addOrMinus * 2;
					hpHeal += data;
					break;
				case 4:
					data = skillLevel * addOrMinus;
					mpHeal += data;
			}
		}
	}
}
